week 2 - Principles of Programming

week 2

Python

  • Released in 1991
  • Python is an interpreted, high-level programming language
  • Used for general-purpose programming.
  • Code is more readable and small amount of code is needed to express concepts compared to other programming languages.
  • Saves time because of the simplicity.

First program - Hello World

code:

print("Hello World")

output:

Hello World
  • Use double quotes "" to print a string

Variables


A variable is a piece of memory that stores a value that can be changed.

  • Variables are names assigned to values.

  • Think of variables as a name given to to containers that holds data.
    Description of image

  • In computer's memory, variable name is the reference to the memory location values are stored.

Variables in Python

Assignment operator

In Python, the primary assignment operator is the equals sign (=), which assigns the value on the right to the variable on the left.

variable <-- value

code:

message = "Hello"
print(message)

output:

Hello

Variable naming

Rules

  • Must begin with a letter or an underscore __ , we cannot use numbers and special symbols as the first character
  • Other characters can be letters, numbers, or __ only, cannot use space and special characters
  • Names are case sensitive
Name is not equal to name, these are two different variables 
  • Reserved words(key words)cannot be used as variable names.
    Description of image

Best practices

  • Be Descriptive and Meaningful
  • Generally lowercase(simple letters)
  • Use Consistent Casing for multi-word naming:
    • camelCase: firstName , totalAmount
    • Use underscore as the delimiter( snake_case ): first_name , total_amount
  • Use shorter meaningful names

Constants

A constant in programming is a named data item that holds a value which cannot be changed during the program's execution.

Constants in Python

Constant naming

  • Capitalized names
  • use __ for multi-word names

Examples:

ID = 2798798
MY_COUNTRY = "Sri Lanka"